
import UIKit
import SnapKit
import SwiftEventBus


class MessageCell : UITableViewCell{
    private let kMaxWidth: CGFloat = kScreenW * 0.75
    var message: Message!
    func bindViewModel(_ message: Message) {
        
        //        progressView.isHidden = true
        if message.isQuestion{
            
            let paraph = NSMutableParagraphStyle()
            //将行间距设置为28
            paraph.lineSpacing = 4
            //样式属性集合
            let attributes = [NSAttributedString.Key.font:UIFont.systemFont(ofSize: 16),
                              NSAttributedString.Key.paragraphStyle: paraph]
            messageLabel.attributedText = NSAttributedString(string: message.question, attributes: attributes)
            
            messageLabel.textColor = .white
            
//            bubbleView.backgroundColor = .primaryColor
//            bubbleView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMinXMaxYCorner, .layerMaxXMinYCorner]
            
            //            let contentSize = messageLabel.sizeThatFits(CGSize(width: kMaxWidth, height: CGFloat(Float.greatestFiniteMagnitude)))
            //
            //            let sizeWidth  = contentSize.width + 12
            //            let sizeHeight = contentSize.height + 8
            //
            //
            //
            //
            //            messageLabel.snp.remakeConstraints { (make) in
            //                make.top.equalToSuperview().offset(24)
            //                make.bottom.equalToSuperview().offset(-15)
            //                make.right.equalToSuperview().offset(-18)
            //                make.width.equalTo(sizeWidth)
            //                make.height.equalTo(sizeHeight)
            //            }
            //
            //
            //            bubbleView.snp.remakeConstraints { (make) in
            //                make.right.equalToSuperview().offset(-12)
            //                make.top.equalTo(messageLabel).offset(-6)
            //                make.bottom.equalTo(messageLabel).offset(6)
            //                make.width.equalTo(messageLabel).offset(18)
            //            }
            //
            
            
        }else{
            if let answer = message.answer{
                let paraph = NSMutableParagraphStyle()
                //将行间距设置为28
                paraph.lineSpacing = 4
                //样式属性集合
                let attributes = [NSAttributedString.Key.font:UIFont.systemFont(ofSize: 16),
                                  NSAttributedString.Key.paragraphStyle: paraph]
                
                messageLabel.attributedText = NSAttributedString(string: answer.isEmpty ? "       " : answer, attributes: attributes)
                messageLabel.textColor = .black
//                bubbleView.backgroundColor = .white
//                bubbleView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner, .layerMaxXMaxYCorner]
//                
                //                let contentSize = messageLabel.sizeThatFits(CGSize(width: kMaxWidth, height: CGFloat(Float.greatestFiniteMagnitude)))
                //
                //                let sizeWidth  = contentSize.width + 12
                //                let sizeHeight = contentSize.height + 8
                //                print("sizeHeight:\(sizeHeight) text:\(messageLabel.text)")
                //                if answer.isEmpty{
                //
                //                    progressView.startAnimating()
                //
                //                }else{
                //                    progressView.stopAnimating()
                //
                //
                //                }
                //
                //                progressView.snp.remakeConstraints { (make) in
                //                    make.top.equalToSuperview().offset(24)
                //                    make.bottom.equalToSuperview().offset(-15)
                //                    make.left.equalToSuperview().offset(24)
                //                    make.width.equalTo(sizeWidth)
                //                    make.height.equalTo(sizeHeight)
                //                }
                //
                //                messageLabel.snp.remakeConstraints { (make) in
                //                    make.top.equalToSuperview().offset(24)
                //                    make.bottom.equalToSuperview().offset(-15)
                //                    make.left.equalToSuperview().offset(24)
                //                    make.width.equalTo(sizeWidth)
                //                    make.height.equalTo(sizeHeight)
                //                }
                //
                //
                //                bubbleView.snp.remakeConstraints { (make) in
                //                    make.left.equalToSuperview().offset(12)
                //                    make.top.equalTo(messageLabel).offset(-6)
                //                    make.bottom.equalTo(messageLabel).offset(6)
                //                    make.width.equalTo(messageLabel).offset(12)
                //                }
                
            }
            
            
            
        }
        
        
    }
    
    
    
    var messageLabel: UILabel = {
        let titelLabel = UILabel()
        titelLabel.font = UIFont.systemFont(ofSize: 15)
        titelLabel.textAlignment = .left
        titelLabel.numberOfLines = 0
        return titelLabel
    }()
    
    var bubbleView: UIView = {
        let view = UIView()
        view.layer.masksToBounds = true
        view.layer.cornerRadius = 18
        return view
    }()
    
    
    
    var progressView: UIActivityIndicatorView = {
        let activityIndicator = UIActivityIndicatorView(style: .medium)
        activityIndicator.color = .primaryColor
        
        return activityIndicator
    }()
    
    
    
    
    
    
    @objc private func handleRightBarButtonTapped(_ sender: Any) {
        
    }
    
    @objc func chatTapped(tapGestureRecognizer: UITapGestureRecognizer)
    {
        //        delegate?.didTapChat(user: user)
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        
//        contentView.addSubview(bubbleView)
        contentView.addSubview(messageLabel)
        contentView.addSubview(progressView)
        self.backgroundColor = commonBackgroundColor
        
        messageLabel.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(24)
            make.bottom.equalToSuperview().offset(-15)
            make.left.equalToSuperview().offset(24)
            make.width.lessThanOrEqualTo(kMaxWidth)
            
        }
        
        
//        bubbleView.snp.makeConstraints { (make) in
//            make.left.equalToSuperview().offset(12)
//            make.top.equalTo(messageLabel).offset(-6)
//            make.bottom.equalTo(messageLabel).offset(6)
//            make.width.equalTo(messageLabel).offset(12)
//        }
        
    }
    
    
}

